package com.chenfy.everysound.ui.main

import android.os.Bundle
import android.util.Log
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chenfy.everysound.R
import com.chenfy.everysound.SoundManager
import com.chenfy.everysound.base.BaseFragment
import com.chenfy.everysound.entity.SoundData
import com.chenfy.everysound.launch
import com.chenfy.everysound.net.DownloadManager
import com.chenfy.everysound.util.checkSound
import com.chenfy.everysound.util.toast
import com.chenfy.everysound.widget.doOnItemClick


/**
 * A placeholder fragment containing a simple view.
 */
class SoundFragment : BaseFragment() {
    private lateinit var contentRv: RecyclerView
    private val soundAdapter = SoundAdapter()


    override fun layout() = R.layout.fragment_main

    override fun initView() {
        contentRv = bindView(R.id.contentRv)
        contentRv.layoutManager = GridLayoutManager(requireContext(), 3)

        // 添加分割线
        ContextCompat.getDrawable(requireContext(),R.drawable.divider)?.let {
            val decorationH = DividerItemDecoration(requireContext(),DividerItemDecoration.HORIZONTAL)
            decorationH.setDrawable(it)
            contentRv.addItemDecoration(decorationH)

            val decorationV = DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL)
            decorationV.setDrawable(it)
            contentRv.addItemDecoration(decorationV)
        }


        contentRv.adapter = soundAdapter

        contentRv.doOnItemClick { vh, view ->
            val pos = vh.adapterPosition
            launch {
                if (checkSound(soundAdapter.datas[pos].sound)) {
                    // 播放声音
                    SoundManager.play(soundAdapter.datas[pos])
                } else {
                    // 下载声音
                    "下载声音资源".toast()
                    soundAdapter.addDownload(pos)
                    DownloadManager.download(soundAdapter.datas[pos])
                    soundAdapter.removeDownload(pos)
                }
            }
            true
        }

        SoundManager.playingItem.observe(this){
            soundAdapter.showPlayingItem(it)
        }
    }


    override fun initData() {
        super.initData()
        val index = arguments?.getInt(ARG_SECTION_NUMBER) ?: 0
        val soundEntity = SoundData.soundEntity[index]
        Log.d("Chen", "soundEntity:${soundEntity}")
        soundAdapter.datas = soundEntity.data
    }


    companion object {
        private const val ARG_SECTION_NUMBER = "section_number"

        @JvmStatic
        fun newInstance(sectionNumber: Int): SoundFragment {
            return SoundFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_SECTION_NUMBER, sectionNumber)
                }
            }
        }
    }
}